package ch01.handler;

/**
 * Created by zc on 2016/11/20.
 */
public abstract class Handler {
    private Handler nextHandler;
    private int level;

    protected void setNextHandler(Handler handler){
        this.nextHandler = handler;
    }

    public final String handlerRequest(Request request){
        if(request.getLevel()==this.level){
            return response(request);
        } else {
            if(this.nextHandler != null){
                return this.nextHandler.handlerRequest(request);
            } else {
                return "请求处理失败";
            }
        }
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    protected abstract String response(Request request);
}
